package com.retallife.user.controller;

import com.retallife.user.exception.Result;
import com.retallife.user.pojo.ShoppingCart.UserAddShoppingCartVo;
import com.retallife.user.pojo.ShoppingCart.UserShoppingCartGoodsNameVo;
import com.retallife.user.pojo.ShoppingCart.UserShoppingCartVo;
import com.retallife.user.service.Goods.GoodsService;
import com.retallife.user.service.ShoppingCart.ShoppingCartService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@CrossOrigin
@Api(tags = "购物车信息类")
@RequestMapping(method = {RequestMethod.POST, RequestMethod.GET})
public class ShoppingCartController {

    @Autowired
    GoodsService goodsService;

    @Autowired
    ShoppingCartService shoppingCartService;

    @ApiOperation(value = "查询购物车表中的所有信息")
    @RequestMapping("FindAllShoppingCartGoods")
    public List<UserShoppingCartVo> FindAllShoppingCartGoods() {
        return shoppingCartService.FindAllShoppingCartGoods();
    }

    @ApiOperation(value = "查询购物车表中的某个用户的信息")
    @RequestMapping("FindShoppingCartGoods")
    public List<UserShoppingCartVo> FindShoppingCartGoods(@RequestBody UserShoppingCartGoodsNameVo userShoppingCartGoodsNameVo) {
        return shoppingCartService.FindShoppingCartGoodsName(userShoppingCartGoodsNameVo);
    }


    @ApiOperation(value = "商品增加进购物车")
    @RequestMapping("AddShoppingCartGoods")
    public Result AddShoppingCartGoods(@RequestBody UserAddShoppingCartVo userAddShoppingCartVo) {
        //判断商品是否重名
        String userShoppingCartGoodsName = userAddShoppingCartVo.getUserShoppingCartGoodsName();
        UserShoppingCartGoodsNameVo userShoppingCartGoodsNameVo = new UserShoppingCartGoodsNameVo();
        userShoppingCartGoodsNameVo.setUserName(userShoppingCartGoodsName);
        List<UserShoppingCartVo> FindShoppingCartGoodsName = shoppingCartService.FindShoppingCartGoodsName(userShoppingCartGoodsNameVo);

        if (FindShoppingCartGoodsName.size()==0) {
            //计算总的金额（租金*时长+押金）*数量
            double userShoppingCartGoodsAllMoney = (userAddShoppingCartVo.getUserShoppingCartGoodsPrice() * userAddShoppingCartVo.getUserShoppingCartGoodsRentTime()
                    + userAddShoppingCartVo.getUserShoppingCartGoodsDeposit())
                    * userAddShoppingCartVo.getUserShoppingCartGoodsAmount();
            userAddShoppingCartVo.setUserShoppingCartGoodsAllMoney(userShoppingCartGoodsAllMoney);

            int result = shoppingCartService.AddShoppingCartGoods(userAddShoppingCartVo);
            if (result > 0) {
                return Result.ok("增加成功");
            }
        }
        return Result.ok("增加失败，商品名重复");
    }

    @ApiOperation(value = "删除购物车中的商品")
    @RequestMapping("DeleteShoppingCartGoods")
    public Result DeleteShoppingCartGoods(@RequestBody UserShoppingCartGoodsNameVo userShoppingCartGoodsNameVo) {
        int result = shoppingCartService.DeleteShoppingCartGoods(userShoppingCartGoodsNameVo);
        if (result > 0) {
            return Result.ok("增加成功");
        }
        return Result.ok("增加失败，商品名重复");
    }
}
